package com.talkweb.newproject.admin.controller;

import com.talkweb.newproject.admin.rest.SysMenuRest;
import com.talkweb.newproject.admin.sysmenu.dto.SysMenuDTO;
import com.talkweb.newproject.api.model.admin.SysMenuParam;
import com.talkweb.newproject.api.model.admin.TreeNodeVo;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qsf on 2019/5/13.
 */
@RestController
@RequestMapping(value="/sysMenu-api")
@Slf4j
public class SysMenuController {

    @Autowired
    private SysMenuRest menuService;

    /**
     * 获取单个业务对象
     * @param ID
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/querySysMenuByPrimaryKey")
    public ResponseResult<SysMenuDTO> querySysMenuByPrimaryKey(@RequestParam("ID") String ID) throws Exception {
        ResponseResult<SysMenuDTO> result = new ResponseResult<>();
        try{
            return menuService.querySysMenuByPrimaryKey(ID);

        }catch(Exception e) {
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    /**
     * 取得list业务对象
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/querySysMenuList",method = RequestMethod.POST)
    public ResponseResult<List<TreeNodeVo>> querySysMenuList(@RequestBody SysMenuParam obj) throws Exception {
        ResponseResult<List<SysMenuDTO>> sysMenuList = new ResponseResult<>();
        ResponseResult<List<TreeNodeVo>> result = new ResponseResult<>();
        try{

            sysMenuList = menuService.querySysMenuList(obj);
            result.setData(this.toTreeModel(sysMenuList.getData()));
            result.setStatus(Status.SUCCESS);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }


    private List<TreeNodeVo> toTreeModel(List<SysMenuDTO> data) {
        List<TreeNodeVo> treeNodeVoList = new ArrayList<>();
        for(SysMenuDTO dto : data) {
            TreeNodeVo vo = new TreeNodeVo();
            vo.setId(dto.getId().toString());
            vo.setName(dto.getMenuName());
            vo.setParentId(dto.getParentId());
            treeNodeVoList.add(vo);
        }
        return TreeNodeVo.treeBuild(treeNodeVoList);
    }

    /**
     * 根据条件查询单个对象
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method = RequestMethod.POST)
    public ResponseResult<SysMenuDTO> queryLike(@RequestBody SysMenuDTO obj) throws Exception {
        ResponseResult<SysMenuDTO> result = new ResponseResult<>();
        try{

            return menuService.queryLike(obj);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 新增数据
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public ResponseResult insertSysMenu(@RequestBody SysMenuDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            if(obj != null && obj.getId()>0) {
               return  menuService.update(obj);
            }else {
               return  menuService.create(obj);
            }
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 更新数据
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/updateSysMenu",method = RequestMethod.POST)
    public ResponseResult updateSysMenu(@RequestBody SysMenuDTO obj) throws Exception{
        ResponseResult result = new ResponseResult();
        try{

            return menuService.update(obj);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value="/deleteSysMenu",method = RequestMethod.POST)
    public ResponseResult deleteSysMenu(@RequestParam("ids") String ids) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            if(ids != null && !"".equals(ids)) {
                menuService.deleteSysMenu(ids);
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功");
            }else{
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败");
            }
            return result;

        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }
}
